//
//  NoticeTableViewCell.m
//  51易借
//
//  Created by 张睿 on 2017/8/31.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "NoticeTableViewCell.h"
#import "NoticeListModel.h"

@interface NoticeTableViewCell ()

@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIImageView * rightImageView;

@end

@implementation NoticeTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel * titleLabel = [UILabel new];
    
//    titleLabel.text = @"51易借正式上线";
    titleLabel.textColor = ColorWithRGB(0x303c41);
        titleLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
//    titleLabel.font = [UIFont fontWithName:TYPE_FACE size:12 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.contentView).offset(30 * SCREENH6);
    }];
    
    
    UILabel * timeLabel = [UILabel new];
    
//    timeLabel.text = @"2016.02.02  23:21:23";
    timeLabel.textColor = ColorWithRGB(0x303c41);
        timeLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
//    timeLabel.font = [UIFont fontWithName:TYPE_FACE size:12 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.titleLabel);
    }];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"倒三角-箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.contentView addSubview:self.rightImageView];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-13 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    
}

- (void)setModel:(NoticeListModel *)model{
    
    _model = model;
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@",model.name];
    self.ID = [NSString stringWithFormat:@"%@",model.id];
    
    NSString * timeString = [self timeWithTimeIntervalString:model.announceTime];
    
    self.timeLabel.text = timeString;
}


- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd  HH:mm:ss"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
